<template>
  <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="物料编码" prop="goodsCode">
          <el-input v-model="queryParams.code" placeholder="请输入物料编码"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入物料名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="物料型号" prop="model">
          <el-input v-model="queryParams.model" placeholder="请输入物料型号"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-table border v-loading="loading" :data="columns" ref="table" @selection-change="handleSelectionChange" @sort-change="handleSortChange" :height="tableMaxHeight">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column sortable label="物料编码" align="center" prop="code"/>
          <el-table-column sortable label="物料名称" align="center" prop="name"/>
          <el-table-column sortable label="物料型号" align="center" prop="model"/>
          <el-table-column sortable label="计量单位" align="center" prop="unit"/>
          <el-table-column label="数量" align="center" prop="materiaCount" width="200">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.materiaCount" :min="0" :precision="3" size="mini"
                               @change="changePriceAll(scope.row)"></el-input-number>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitGoods" v-if="">提交</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {deleteAction, downloadFile, getAction, addAction} from '@/api/manage'

export default {
  name: "PiliangGoodsOrderInForm",
  dicts: ['fund_callback', 'sale_type', 'yes_no'],
  components: {},
  data() {
    return {
      title: "批量材料选择",
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      mainIds: [],
      rows: [],
      columns: [],
      rules: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售信息管理表格数据
      dataList: [],
      form: {},
      gsaleId: null,
      ironDetailId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        mainId: null,
        name: null,
        code: null,
        brand: null,
        unit: null,
        costPrice: null,
        materiaCount: null,
        count: null,
        cost: null,
        validityTerm: null,
        buyId: null,
        buyName: null,
        buyDate: null,
        buyType: null,
        reimburse: null,
        supplierId: null,
        supplierName: null,
        dateRange: "",
        params: {},
      },
      url: {
        list: '/goodsOrderIn/goodsOrderIn/list',
        insertForMaterialPiliang: '/goodsSaleMaterial/goodsSaleMaterial/insertForMaterialPiliang',
      },
      PrjNameOptions: [],
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    // this.intDictsForPrj();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight);
  },
  methods: {
     /** 排序触发事件 */
     handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.dataList = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    changePriceAll(row) {
      let that = this;
      let handleReckonMoney = (row) => {
        let row_money = 0;
        row_money = that.functionNumberMul(Number(row.materiaCount), Number(row.costPriceTax))
        that.$set(row, 'costTax', row_money);
      };
      handleReckonMoney(row);
    },
    functionNumberMul(arg1, arg2) {
      var m = 0;
      var s1 = arg1.toString();
      var s2 = arg2.toString();
      try {
        m += s1.split(".")[1].length;
      } catch (e) {
      }
      try {
        m += s2.split(".")[1].length;
      } catch (e) {
      }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    },
    submitGoods() {
      let that = this;
      const ids = this.ids;
      if (ids == null || ids == '' || ids == 'undefined') {
        that.$modal.msgError("请选择一条记录");
        return;
      }

      let arrays = that.dataList;
      // for (let i = 0; i < arrays.length; i++) {
      //   if(arrays[i].materiaCount===0){
      //     this.$message({
      //       message: '请填写所需材料'+arrays[i].name+'数量。',
      //       type: 'warning'
      //     });
      //     return;
      //   }
      // }

      that.$refs["form"].validate(valid => {
        if (valid) {
          const params = Object.assign({});
          params.mainIds = that.mainIds;
          params.ironDetailId = that.ironDetailId;
          params.columns = that.dataList;
          addAction(that.url.insertForMaterialPiliang, params).then(response => {
            that.$modal.msgSuccess("添加成功！");
            that.open = false;
            that.$emit('ok');
          });
        }
      });
    },
    handleAdd() {
      this.$refs.goodsOrderInForm.addGoodsSale();
    },
    handleUpdate(row) {
      this.$refs.goodsOrderInForm.handleUpdate(row);
    },
    handleView(row) {
      this.$refs.goodsOrderInForm.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      deleteAction(this.url.delete, row.id).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    showOrderInByGoods(ids,gsaleId) {
      console.log(ids,gsaleId,'ids,gsaleId 产品选择物料产品');
      let that = this;
      that.mainIds = ids;
      that.gsaleId = gsaleId;
      that.ironDetailId = null;
      that.getList();
      that.open = true;
    },

    showOrderInByIron(ids,gsaleId) {
      console.log(ids,gsaleId,'ids,gsaleId 铁附件选择物料产品');
      let that = this;
      that.ironDetailId = ironDetailId;
      that.gsaleId = null;
      that.getList();
      that.open = true;
    },

    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 查询销售信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  }
};
</script>
